package fictional.GUI;

import javax.swing.JOptionPane;

import org.jdesktop.application.Application;
import org.jdesktop.application.Task;

import fictional.Common.IBL;
import fictional.Common.Response;

/**
 * This is the default task which all other tasks extend. 
 * Holds general behavior for when the tasks either fails
 * or succeeds. 
 */
public class DefaultTask extends Task<Object, Void> {
	
	public DefaultTask(Application application) {
		super(application);
	}
	
	protected Response response;
	protected String errorTitle;
	protected boolean isSuccessMessageNeeded;
	protected String successMessage;
    protected IBL BL;
    protected GUI gui;
    protected boolean isActionDone;
	
	@Override
	protected Object doInBackground() throws Exception {
		return null;
	}
	
	public boolean isActionDone()
	{
		if (!isActionDone)
		{
			isActionDone = true;
			return false;
		}
		return true;
	}

	public void doInCaseOfFail()
	{
		if (response.getMessage() == null)
		{
			JOptionPane.showMessageDialog(null, response.getException().getMessage(), errorTitle, JOptionPane.ERROR_MESSAGE);	
		}
		else
		{
			JOptionPane.showMessageDialog(null, response.getMessage(), errorTitle, JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public void doInCaseOfSuccess()
	{
		if (isSuccessMessageNeeded)
		{
			JOptionPane.showMessageDialog(null, successMessage, "Operation Successful", JOptionPane.INFORMATION_MESSAGE);
		}
	}
	
	public boolean isSuccessful()
	{
		return response.isIs_Success();
	}
	
	public String getErrorMessage()
	{
		return response.getMessage();
	}
	
	public String getErrorTitle()
	{
		return errorTitle;
	}
}
